package sign

import (
	"fmt"
	"html/template"
	"net/http"
	"errors"
	"strings"
	"time"
	"appengine"
	"appengine/datastore"
	"rwxun/util"
)


type User struct {
	User				string
	Psw					string
	Time				time.Time
	SessionCookieKey	string
	SessionExpire		time.Time
}

var cacheTPL = make(map[string]*template.Template)

func Home(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type","text/html;charset=utf-8")
	fmt.Fprintf(w, "%v", r.URL.Path)
}

func Signupform(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type","text/html;charset=utf-8")

	err := errors.New("")
	if _, ok := cacheTPL["signupform"]; !ok {
		cacheTPL["signupform"], err = template.ParseFiles("rwxun/template/signup/signupform.html")
		if err != nil {
			fmt.Fprint(w, err)
			return
		}
	}
	err = cacheTPL["signupform"].Execute(w, r.FormValue("callback"))
	if err != nil {
		fmt.Fprint(w, err)
	}
}

func Signup(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type","text/html;charset=utf-8")

	user := strings.TrimSpace(r.FormValue("user"))
	psw := strings.TrimSpace(r.FormValue("psw"))
	repsw := strings.TrimSpace(r.FormValue("repsw"))

	if "" == user || "" == psw {
		fmt.Fprint(w, "<script type=\"text/javascript\">alert(\"帐号/密码不能为空！\");history.go(-1);</script>")
		return
	}
	if repsw != psw {
		fmt.Fprint(w, "<script type=\"text/javascript\">alert(\"两次输入的密码不一致！\");history.go(-1);</script>")
		return
	}

	ctx := appengine.NewContext(r)

	q := datastore.NewQuery("User").Filter("User =", user).Limit(1)
	count, err := q.Count(ctx)
	if err != nil {
		fmt.Fprint(w, err)
	}

	if count > 0 {
		fmt.Fprint(w, "<script type=\"text/javascript\">alert(\"帐号已经存在！\");history.go(-1);</script>")
		return
	}

	dataUser := new(User)
	dataUser.User = user
	dataUser.Psw = util.Md5(psw)
	dataUser.Time = time.Now()

	_, err = datastore.Put(ctx, datastore.NewKey(ctx, "User", util.Md5(user), 0, nil), dataUser)
	if err == nil {
		callback := strings.TrimSpace(r.FormValue("callback"))
		if "" == callback {
			callback = "/"
		}
		http.Redirect(w, r, callback, http.StatusFound)
	}else{
		fmt.Fprintf(w, "<script type=\"text/javascript\">alert(\"%s\");history.go(-1);</script>",err)
	}
}

func Signinform(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type","text/html;charset=utf-8")

	err := errors.New("")

	if _, ok := cacheTPL["signinform"]; !ok {
		cacheTPL["signinform"], err = template.ParseFiles("rwxun/template/signup/signinform.html")
		if err != nil {
			fmt.Fprintf(w, "<script type=\"text/javascript\">alert(\"%s\");history.go(-1);</script>",err)
			return
		}
	}
	err = cacheTPL["signinform"].Execute(w, r.FormValue("callback"))
	if err != nil {
		fmt.Fprintf(w, "<script type=\"text/javascript\">alert(\"%s\");history.go(-1);</script>",err)
	}
}

func Signin(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type","text/html;charset=utf-8")
	user := strings.TrimSpace(r.FormValue("user"))
	psw := strings.TrimSpace(r.FormValue("psw"))

	if "" == user || "" == psw {
		fmt.Fprint(w, "<script type=\"text/javascript\">alert(\"帐号/密码不能为空！\");history.go(-1);</script>")
		return
	}

	ctx := appengine.NewContext(r)

	Users := make([]User,0,1)
	q := datastore.NewQuery("User").Filter("User =", user).Limit(1)
	q.GetAll(ctx,&Users)
	if len(Users) < 1{
		fmt.Fprint(w, "<script type=\"text/javascript\">alert(\"帐号不存在！\");history.go(-1);</script>")
		return
	}
	if Users[0].Psw != util.Md5(psw) {
		fmt.Fprint(w, "<script type=\"text/javascript\">alert(\"密码不正确！\");history.go(-1);</script>")
		return
	}

	callback := strings.TrimSpace(r.FormValue("callback"))
	if "" == callback {
		callback = "/"
	}
	http.Redirect(w, r, callback, http.StatusFound)
}
